package main.car;

import lombok.extern.slf4j.Slf4j;
import main.model.ElaModel;
import main.model.ElaRepository;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Configuration
@Slf4j
public class Comment {

    @Resource
    private ElaRepository repository;

    @Value("http://www.chinaautonews.com.cn")
    private String baseUrl;

    @Resource
    private WebDriver driver;

    @Scheduled(fixedDelay = 2000000)
    public void main(){
        driver.get(baseUrl+"/list-11-1.html");
        Document document = Jsoup.parse(driver.getPageSource());
        Element element = document.selectFirst("#pages > a:nth-last-child(2)");
        Elements elements;
        int totalPage = Integer.parseInt(element.text());
        Set<String> urls = new HashSet<>();
        for (int i = 0; i < (totalPage>30?30:totalPage); i++) {
            driver.get(baseUrl+"/list-11-"+(i+1)+".html");
            document = Jsoup.parse(driver.getPageSource());
            elements = document.select("div.lside_nr > ul > li > a:nth-child(1)");
            for (Element e : elements) {
                urls.add(e.attr("href"));
            }
        }
        System.out.println(urls.size());
        saveInfo(driver,urls);
    }

    private void saveInfo(WebDriver driver,Set<String> urls){
        log.info("startAll........ ==>{}",urls.size());
        int c = 1;
        ElaModel elaModel;
        Document document;
        Element element;
        for (String s : urls) {
            try {
                log.info("start..{},==>{}",s,c++);
                elaModel = new ElaModel();
                driver.get(s);
                document = Jsoup.parse(driver.getPageSource());
                //标题
                element = document.selectFirst("div.lside > h1");
                elaModel.setTitle(element.text());
//                elaModel.setId(element.text().hashCode() ^ (element.text().hashCode() >> 16));
                elaModel.setCreateTime(new Date());
                System.out.println(element.text());
                //来源时间
                element = document.selectFirst("div.f12.c999.mb20.source");
                String[] sourceAndTime = element.text().trim().split("\\|");
                elaModel.setSource(sourceAndTime[0]);
                elaModel.setTime(sourceAndTime[1]);
                System.out.println(element.text());
                //简介
                element = document.selectFirst("body > div.wrap > div.center > div.lside > div.desc");
                if (element!=null) {
                    elaModel.setBrief(element.text().trim());
                    System.out.println(element.text());
                }
                //内容
                element = document.selectFirst("div.detail-content");
                elaModel.setContent(element.html());
                elaModel.setType(4);
                repository.save(elaModel);
                log.info("end.....");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        String c = "wedqwfa|fdsfsdfsa";
//        System.out.println(c.split("\\|").length);
//    }
}
